home *** CD-ROM | disk | FTP | other *** search
- /*******************************************\
- * *
- * simtest.c = Test program for SIMPP: *
- * Simple IMage Processing Package. *
- * Copyright (c) 1987, Benjamin M. Dawson *
- * Edit Version: 1.1 : Jan-29-87 *
- * *
- \*******************************************/
-
- #include <stdio.h>
- #include "simpp.h"
-
- /* These error flags are returned by the exit() routine. I have used the
- * values appropriate for UNIX. These values may not give the desired
- * results under VMS, RT-11, etc., so you may have to change them to
- * values appropriate for your system.
- */
- #define ERROR_EXIT 1 /* Signal ERROR exit */
- #define OK_EXIT 0 /* Signal no error exit */
-
- /***** Kernels *****/
- static int kersh1[] = {-1, -1, -1, /* Kernel for sharpening */
- -1, 9, -1,
- -1, -1, -1,};
-
- static int kersh2[] = {-1, -1, -1, /* Kernel for sharpening */
- -1, 10, -1, /* Not as strong as #1 */
- -1, -1, -1,}; /* Use scale factor of 1 */
-
- static int kersh3[] = {-1, -1, -1, /* Kernel for sharpening */
- -1, 12, -1, /* Not as strong as #1 or #2 */
- -1, -1, -1,}; /* Use a scale of 2 */
-
- static int kerhoriz[] = { -1, -1, -1, -1, -1, /* Kernel for horiz. edges */
- 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1,};
-
- static int kervert[] = { -1, 0, 1, /* Kernel for vertical edges */
- -1, 0, 1,
- -1, 0, 1,
- -1, 0, 1,
- -1, 0, 1,};
-
- static int kerlapla[] = {-1, -1, -1, /* Kernel for laplacian */
- -1, 8, -1,
- -1, -1, -1,};
-
- static int kerblur[] = { 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- };
-
- static long h[PIXEL_SIZE] = 0L; /* Histogram array */
- static char name[80] = 0; /* File name */
-
- #define MAX_QUAD 5 /* Maximum quadrant number */
-
- /* Define "quadrants" of image memory, plus "quadrant 0" for the
- entire image memory. Used as a shorthand for image areas */
- #define QUAD0 XSTART,YSTART,XSIZE,YSIZE
- #define QUAD1 XSIZE/2,YSTART,XSIZE/2,YSIZE/2
- #define QUAD2 XSTART,YSTART,XSIZE/2,YSIZE/2
- #define QUAD3 XSTART,YSIZE/2,XSIZE/2,YSIZE/2
- #define QUAD4 XSIZE/2,YSIZE/2,XSIZE/2,YSIZE/2
- #define QUAD5 XSIZE/4,YSIZE/4,XSIZE/2,YSIZE/2
-
- static int qmap[MAX_QUAD+1][4] ={ /* Quadrant array map */
- { QUAD0 }, /* Entire screen */
- { QUAD1 }, /* Upper right */
- { QUAD2 }, /* Upper left */
- { QUAD3 }, /* Lower left */
- { QUAD4 }, /* Lower right */
- { QUAD5 }, /* Center */
- };
- /* Define macro to use quad map */
- #define QUADMAC(i) qmap[i][0],qmap[i][1],qmap[i][2],qmap[i][3]
-
- /* ================================================================ */
-
- main()
- {
- register int i; /* General variable */
- int x,y,z; /* Location and value variables */
- int out; /* Output quadrant */
- char s[20]; /* Input command string */
- char sharp[5]; /* Sharpen command string */
- float xs,ys; /* Stretch factors */
- float gbk,gbl,gbm; /* Gaussian burn factors */
-
- /* Salutations! */
- printf("****** SIMPP test program V1.10 ******\n");
- printf("Copyright (c) 1987, Benjamin M. Dawson\n\n");
-
- /* Open hardware */
- printf("-- Open hardware --\n");
- if (sim_open() == ERROR) {
- printf("<simtest> Can't open hardware!\n");
- exit(ERROR_EXIT);
- }
-
- /* Linearize LUTS, if you have them */
- lin_luts();
- printf("\n");
-
- for (;;) {
-
- /* Print menu */
- printf("Acquire\t Binarize Brighten Burn\n");
- printf("Blur\t Clear area\tCopy area Contrast Stretch\n");
- printf("Grid\t Histogram Horizontal Edges\n");
- printf("Label Laplacian Linear LUTs\n");
- printf("Negate\t Plot Histogram Print Histogram\n");
- printf("Quit Rotate Restore image\n");
- printf("Save image Sharpen Spectrum\n");
- printf("Stretch\t Test all Vertical Edges\n");
- printf("Command>>> ");
-
- scanf("%s",s); /* Get command string, with appended NULL */
-
- /* Match command */
- if (matches(s,"acquire",4)) acquire();
- else if (matches(s,"binarize",4)) {
- i = get_quad();
- binarize(QUADMAC(i),get_int("Threshold ="));
- }
- else if (matches(s,"brighten",4)) {
- i = get_quad();
- brighten(QUADMAC(i),get_int("Value to add="));
- }
- else if (matches(s,"blur",4)) {
- i = get_quad();
- convolve(QUADMAC(i),8,8,kerblur,6,POSITIVE);
- }
- else if (matches(s,"burn",4)) {
- i = get_quad();
- printf("Burn factor (real) =");
- scanf("%f",&gbk);
- printf("Space constant (real) =");
- scanf("%f",&gbl);
- printf("Offset (real) =");
- scanf("%f",&gbm);
- gauss_burn(QUADMAC(i),(double)gbk,(double)gbl,(double)gbm);
- }
- else if (matches(s,"clear",4)) {
- i = get_quad();
- clear_area(QUADMAC(i),(PIXEL)get_int("Clear to intensity ="));
- }
- else if (matches(s,"copy",4)) {
- printf("Copy ");
- i = get_quad();
- printf("To ");
- out = get_quad();
- copy_area(QUADMAC(i),QUADMAC(out));
- }
- else if (matches(s,"contrast",4)) {
- i = get_quad();
- cstretch(QUADMAC(i),get_int("Minimum bin count ="));
- }
- else if (matches(s,"grid",4)) {
- x = get_int("X spacing =");
- y = get_int("Y spacing =");
- z = get_int("Intensity =");
- draw_grid(x,y,(PIXEL)z);
- }
- else if (matches(s,"histogram",4)) {
- i = get_quad();
- histogram(QUADMAC(i),h);
- }
- else if (matches(s,"horizontal",4)) {
- i = get_quad();
- convolve(QUADMAC(i),5,3,kerhoriz,0,ABSOLUTE);
- }
- else if (matches(s,"label",4)) {
- i = get_quad();
- x = get_int("Minimum area size =");
- label(QUADMAC(i),MINPIX,MAXPIX,x,
- (PIXEL)1,(PIXEL)(PIXEL_SIZE-2));
- }
- else if (matches(s,"laplacian",4)) {
- i = get_quad();
- convolve(QUADMAC(i),3,3,kerlapla,0,ABSOLUTE);
- }
- else if (matches(s,"linear",4)) lin_luts();
- else if (matches(s,"negate",4)) {
- i = get_quad();
- negate(QUADMAC(i));
- }
- else if (matches(s,"plot",4)) {
- i = get_quad();
- plot_histo(QUADMAC(i),h,
- (PIXEL)get_int("Intensity to plot with = "));
- }
- else if (matches(s,"print",4)) print_histogram(h);
- else if (matches(s,"quit",4)) {
- sim_close();
- exit(OK_EXIT);
- }
- else if (matches(s,"restore",4)) {
- i = get_quad();
- printf("File name:");
- scanf("%s",name);
- read_image(QUADMAC(i),name);
- }
- else if (matches(s,"rotate",4)) {
- i = get_quad();
- rotate(QUADMAC(i));
- }
- else if (matches(s,"save",4)) {
- i = get_quad();
- printf("File name:");
- scanf("%s",name);
- save_image(QUADMAC(i),name);
- }
- else if (matches(s,"sharpen",4)) {
- i = get_quad();
- printf("Degree of sharpening: High, Medium, or Low:");
- scanf("%s",sharp);
- if (matches(sharp,"high",3))
- convolve(QUADMAC(i),3,3,kersh1,0,POSITIVE);
- else if (matches(sharp,"medium",3))
- convolve(QUADMAC(i),3,3,kersh2,1,POSITIVE);
- else if (matches(sharp,"low",3))
- convolve(QUADMAC(i),3,3,kersh3,2,POSITIVE);
- }
- else if (matches(s,"spectrum",4)) p_spectrum();
- else if (matches(s,"stretch",4)) {
- i = get_quad();
- printf("X stretch factor (real) = ");
- scanf("%f",&xs);
- printf("Y stretch factor (real) = ");
- scanf("%f",&ys);
- stretch(QUADMAC(i),(double)xs,(double)ys);
- }
- else if (matches(s,"test",4)) test_all();
- else if (matches(s,"vertical",4)) {
- i = get_quad();
- convolve(QUADMAC(i),3,5,kervert,0,ABSOLUTE);
- }
-
- else printf("?? Not a valid command!\n");
- }
- }
-
- /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
- static VOID test_all() /* Script to test the package */
- {
- register int i;
- char c;
-
- /* Point operations */
- printf("\n Test Point operations:\n");
- printf("-- Take a picture --\n"); acquire();
- printf("-- Negate second Quadrant --\n"); negate(QUAD2);
- printf("-- Brighten first Quadrant by 30 --\n"); brighten(QUAD1,30);
- printf("-- Contrast stretch third Quadrant --\n"); cstretch(QUAD3,30);
-
- #ifdef LUTS
- printf("\nTest Output LUTs:\n");
- p_spectrum(); /* Set luts to spectrum */
- printf("Type return (ENTER) to continue:");
- scanf("%c",&c);
- lin_luts(); /* Linearize LUTS */
- #endif
-
- /* Area operations */
- printf("\nTest Area operations:\n");
-
- printf("-- Take a picture --\n"); acquire();
- printf("-- Sharpen image in Quadrant 1 --\n");
- convolve(QUAD1,3,3,kersh1,0,POSITIVE);
- printf("-- Horizontal edges in Quadrant 2 --\n");
- convolve(QUAD2,5,3,kerhoriz,0,ABSOLUTE);
- printf("-- Vertical edges in Quadrant 3 --\n");
- convolve(QUAD3,3,5,kervert,0,ABSOLUTE);
- printf("-- Laplacian of Qandrant 4 --\n");
- convolve(QUAD4,3,3,kerlapla,0,ABSOLUTE);
-
- printf("Area operations test done.\n");
- printf("Type return (ENTER) to continue:");
- scanf("%c",&c);
-
- /* Geometric operations */
- printf("\nTest Geometric operations:\n");
- printf("-- Take a picture --\n"); acquire();
- printf("-- Draw calibration grid --\n"); draw_grid(10,15,MAXPIX);
- printf("-- Rotate Quadrant 2 --\n"); rotate(QUAD2);
- printf("-- Stretch Quadrant 1 by 2.0 in X --\n");
- stretch(QUAD1,2.0,1.0);
- printf("-- Stretch Quadrant 3 by 2.0 in Y --\n");
- stretch(QUAD3,1.0,2.0);
- printf("-- Stretch Quadrant 4 by 3 in X and Y (ZOOM) --\n");
- stretch(QUAD4,3.0,3.0);
-
- printf("Geometric operations test done.\n");
- printf("Type return (ENTER) to continue:");
- scanf("%c",&c);
-
- /* Measurement operations */
- printf("\nTest Measurement operations:\n");
- printf("-- Taking a picture --\n"); acquire();
- printf("-- Histogram Quadrant 2 --\n"); histogram(QUAD2,h);
- print_histogram(h);
- printf("-- Plot histogram in Quadrant 1 --\n");
- clear_area(QUAD1,MINPIX);
- plot_histo(QUAD1,h,MAXPIX);
-
- printf("-- Copy Quad 3 to Quad 4 --\n");
- copy_area(QUAD3,QUAD4);
- printf("-- Binarize Quadrants 3 and 4 --\n");
- binarize(QUAD3,PIXEL_SIZE/2);
- binarize(QUAD4,PIXEL_SIZE/2);
-
- #ifdef LUTS /* If you have LUTS, use them to color image */
- printf("-- Setting LUTs to show labeled areas --\n");
- p_spectrum();
- /* Leave the binary colors (MINPIX and MAXPIX) alone */
- write_LUT(RED,MAXPIX,MAXPIX);
- write_LUT(GREEN,MAXPIX,MAXPIX);
- write_LUT(BLUE,MAXPIX,MAXPIX);
- write_LUT(RED,MINPIX,MINPIX);
- write_LUT(GREEN,MINPIX,MINPIX);
- write_LUT(BLUE,MINPIX,MINPIX);
- #endif
- printf("Label binary objects with 20 pixels or more in Quadrant 4\n");
- label(QUAD4,MINPIX,MAXPIX,20,(PIXEL)1,(PIXEL)(PIXEL_SIZE-2));
- printf("Measurement operations test done.\n");
-
- printf("Type return (ENTER) to continue:");
- scanf("%c",&c);
-
- /* Utility operations */
- printf("\Test Utility operations:\n");
- printf("-- Take a picture --\n"); acquire();
-
- printf("-- Save Quadrant 2 --\n");
- printf("File name:");
- scanf("%s",name);
- save_image(QUAD2,name);
- printf("-- Restore Quadrant 2 image to Quadrant 4 --\n");
- read_image(QUAD4,name);
-
- printf("Utility operations test done.\n");
- printf("\n TEST DONE\n");
-
- }
-
- /* ======================= Internal routines ======================= */
-
- /* WARNING: This assumes an ASCII encoding for your characters!!! */
- /* Encode and match two strings, up to string length n or a null.
- Returns 1 if match, 0 elsewise */
- static int matches(s1,s2,n)
- char *s1,*s2; /* Strings to match */
- int n; /* Number of characters to use */
- {
- register int i,v;
-
- /* If either string is NULL, return no match */
- if ((*s1 == NULL) || (*s2 == NULL)) return(0);
-
- v = 0;
- for (i = 0 ; i < n ; i++) {
- /* Nulls always match after one character */
- if ((*s1 == NULL) || (*s2 == NULL)) return(1);
- /* Lower case elements. REQUIRES ASCII ENCODING!!! */
- if ((*s1 > 0100) && (*s1 <= 0132)) *s1 += 040;
- if ((*s2 > 0100) && (*s2 <= 0132)) *s2 += 040;
- if (*s1++ != *s2++) return(0);
- }
- return(1);
- }
- /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
- static int get_quad() /* Get a quadrant number (0,1,2,3,4) */
- {
- int q;
-
- for (;;) {
- printf("Quadrant number = ");
- scanf("%d",&q);
- if ((q < 0) || (q > MAX_QUAD))
- printf("Quadrant number must be 0 (for all) to %d!\n",
- MAX_QUAD);
- else
- return(q);
- }
- }
-
- /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
- int get_int(s) /* Print string and return an integer value */
- char *s;
- {
- int i;
-
- printf("%s ",s);
- scanf("%d",&i);
- return(i);
- }
-
-
- /* ================ End of simtest.c ================ */
-
- /* <-- FILE BREAK --> */